﻿using System;
using System.Configuration.Provider;
using System.Linq;
using CID.Domain.Users;
using NailsFramework;
using NailsFramework.Persistence;

namespace CID.Web.Support
{
    public sealed class RoleProvider : System.Web.Security.RoleProvider
    {
        #region Static Members
        private static readonly string[] roleNames;
        
        static RoleProvider()
        {
            roleNames = Enum.GetNames(typeof(UserRole));
        }

        private static UserRole GetUserRole(string roleName)
        {
            if (roleNames.Contains(roleName)) throw new ProviderException(String.Format("The Role {0} does not exist", roleName));
            return (UserRole)Enum.Parse(typeof(UserRole), roleName);
        }
        #endregion

        private IBag<User> Users { get; set; }

        public RoleProvider()
        {
            Users = Nails.ObjectFactory.GetObject<IBag<User>>();
        }

        private User GetUser(string username)
        {
            var result = Users.SingleOrDefault(x => x.UserName == username);
            if (result == null) throw new ProviderException(String.Format("Unable to find user {0}", username));
            return result;
        }

        public override void AddUsersToRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override string ApplicationName
        {
            get { return "CID"; }
            set { throw new NotImplementedException(); }
        }

        public override void CreateRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool DeleteRole(string roleName, bool throwOnPopulatedRole)
        {
            throw new NotImplementedException();
        }

        public override string[] FindUsersInRole(string roleName, string usernameToMatch)
        {
            throw new NotImplementedException();
        }

        public override string[] GetAllRoles()
        {
            return roleNames;
        }

        public override string[] GetRolesForUser(string username)
        {
            var user = GetUser(username);

            return user.AssignedRoles.Select(x => x.UserRole.ToString()).ToArray();
        }

        public override string[] GetUsersInRole(string roleName)
        {
            throw new NotImplementedException();
        }

        public override bool IsUserInRole(string username, string roleName)
        {
            var user = GetUser(username);
            var userRole = GetUserRole(roleName);

            return user.Is(userRole);
        }

        public override void RemoveUsersFromRoles(string[] usernames, string[] roleNames)
        {
            throw new NotImplementedException();
        }

        public override bool RoleExists(string roleName)
        {
            return roleNames.Contains(roleName);
        }
    }
}